/* $RCSFile: ADNDynXmlView.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynXmlView.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

/* Copyrights Christian Surieux 2010 */
/* Class ADNDynXmlView */
/* Date 2010-01-10 19:46:49Z */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.IO;

using CrmSdk;
using System.Data;

namespace AddonNice.CRMWrapper
{
    /// <summary>
    /// Common ancestor to UserQuery and SavedQuery
    /// </summary>
    public class ADNDynXmlView : DynEntity
	{
		public ADNDynXmlView(): base(){}
		public ADNDynXmlView(CRMWrapperBase w, string schemanm): base(w, schemanm)
		{}
		public ADNDynXmlView(CRMWrapperBase w, string schemanm,Guid CrmId): base(w,schemanm, CrmId){}
		public ADNDynXmlView(CRMWrapperBase w,string schemanm, InternalDynamicEntity e): base(w,schemanm,e,null,null){}
		public ADNDynXmlView(CRMWrapperBase w, string schemanm,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s): base(w,schemanm,e,prop,s){}
		public ADNDynXmlView(ResetWrapperHandler fn, string schemanm): base(fn,schemanm)
		{}
		public ADNDynXmlView(ResetWrapperHandler fn, string schemanm,Guid CrmId): base(fn,schemanm,CrmId){}
		public ADNDynXmlView(ResetWrapperHandler fn,string schemanm,InternalDynamicEntity e): base(fn,schemanm,e,null,null){}
		public ADNDynXmlView(ResetWrapperHandler fn,string schemanm,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s): base(fn,schemanm,e,prop,s){}

		/* Properties */

		public override string PrimaryFieldName
		{
		    get	{	return CrmStr.name;}
		    set	{}
		}

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return name;
            }
            set
            {
                name =   value;
            }
        }

		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public string columnsetxml
		{
		    get	{	 return StringPropertyValue(CrmStr.columnsetxml);}
		    set	{	AddStringProperty(CrmStr.columnsetxml, value);}
		}

		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public string description
		{
		    get	{	 return StringPropertyValue(CrmStr.description);}
		    set	{	AddStringProperty(CrmStr.description, value);}
		}

		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public string fetchxml
		{
		    get	{	 return StringPropertyValue(CrmStr.fetchxml);}
		    set	{	AddStringProperty(CrmStr.fetchxml, value);}
		}

		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public string layoutxml
		{
		    get	{	 return StringPropertyValue(CrmStr.layoutxml);}
		    set	{	AddStringProperty(CrmStr.layoutxml, value);}
		}

		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public override string name
		{
		    get	{	 return StringPropertyValue(CrmStr.name);}
		    set	{	AddStringProperty(CrmStr.name, value);}
		}

        /// <summary>
        /// Cf InternalSavedQueryType
        /// </summary>
		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public int querytype
		{
		    get	{	 return NumberPropertyValue(CrmStr.querytype);}
		    set	{	AddNumberProperty(CrmStr.querytype, value);}
		}

        /// <summary>
        /// Entity returned by query
        /// </summary>
		[CrmDynAttribute(CrmStr.userquery),CrmDynAttribute(CrmStr.savedquery)]
		public string returnedtypecode
		{
		    get	{	 return StringPropertyValue(CrmStr.returnedtypecode);}
		    set	{	AddStringProperty(CrmStr.returnedtypecode, value);}
		}

        public InternalColumnSet ExtractColumns()
        {
            //string xmlstr   =   "<columnsetxml><columnset><column>new_name</column><column>createdon</column><ascend>new_name</ascend></columnset></columnsetxml>";
            StringReader sr         =   new StringReader(columnsetxml);
            XElement xmlcolumnset   =   XElement.Load(sr);
            var elem                =   from c in xmlcolumnset.Elements("columnset").Elements("column") select new { ColName = c.Value };
            int count               =   elem.Count();
            InternalColumnSet colset    =   new InternalColumnSet();
            colset.Attributes       =   new string[count];
            int i                   =   0;
            foreach ( var e in elem )
                colset.Attributes[i++]  =   e.ColName;
            sr.Close();
            return colset;
        }

        public DataTable ExecuteAsTb(bool isAdmin,ResetWrapperHandler fn)
        {
            return wrapper.Util.GetTableFromQuery(this,fn,isAdmin);

        }
	}
}